package pe.intix.drawback.model;

import java.util.Date;

import javax.persistence.*;
import javax.validation.constraints.*;

import org.openxava.annotations.*;
import org.openxava.calculators.*;
import org.openxava.model.*;

import pe.intix.drawback.util.*;

/**
 * @author Jeromy Altuna
 * @author Renato
 */
@Entity
@Views([
	@View(members = "tipoComprobante; serie, numero, fecha; imagenes, hayImagen"),
	
	@View(name = "SerieNumeroFecha", members = "serie, numero, fecha")	
])
@Table(name = "comprobante_pago")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "id_naturaleza_comprobante", 
					 discriminatorType = DiscriminatorType.INTEGER)
abstract public class ComprobantePago extends ConImagenes{
	
	@Hidden
	@Id @GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id_comprobante")
	Long id
	
	@Required @Editor("FillFourZeros")
	@Column(name = "serie", length = 4)
	String serie

	@Required @Editor("FillSevenZeros")
	@Column(name = "numero", length = 7)
	String numero

	@Required
	@Column(name = "fecha")
	@Temporal(TemporalType.TIMESTAMP)
	Date fecha

	@Stereotype("MEMO")
	@Column(name = "glosa")
	String glosa

	@Required @DefaultValueCalculator(ZeroBigDecimalCalculator)
	@Digits(integer = 14, fraction = 6)
	@Column(name = "valor_venta", precision = 14, scale = 6)
	BigDecimal valorVenta
	
	@Column(name = "impuestos", precision = 14, scale = 6)
	BigDecimal impuestos = new BigDecimal("0.0")

	@Required @DefaultValueCalculator(ZeroBigDecimalCalculator)
	@Digits(integer = 14, fraction = 6)
	@Column(name = "importe_total", precision = 14, scale = 6)
	BigDecimal importeTotal

	@Hidden
	@Column(name = "id_nisira", length = 15)
	String idNisira

	@DescriptionsList(descriptionProperties = "descTablaTipo",
		condition = "tablaTipo = 'tipo_moneda'")
	@JoinColumn(name = "id_tipo_moneda", referencedColumnName = "id_tipo")
	@ManyToOne(fetch=FetchType.LAZY, optional = false) //es false
	Tipo tipoMoneda

	@DescriptionsList(descriptionProperties = "descTablaTipo",
		condition = "tablaTipo = 'tipo_comprobante'")
	@JoinColumn(name = "id_tipo_comprobante", referencedColumnName = "id_tipo")
	@ManyToOne(fetch=FetchType.LAZY, optional = false)
	Tipo tipoComprobante

	//	@OneToMany(cascade = CascadeType.ALL, mappedBy = "comprobantePago")
	//	private List<DetalleDuaFacturaExportacion> detalleDuaFacturaExportacionList;


	//  @JoinColumn(name = "id_naturaleza_comprobante", referencedColumnName = "id_tipo")
	//  @ManyToOne(fetch=FetchType.LAZY, optional = false)
	//	Tipo naturalezaComprobante

	//	@JoinColumn(name = "id_proveedor", referencedColumnName = "id_proveedor", nullable = false, insertable = false, updatable = false)
	//	@ManyToOne(optional = false)
	//	private Proveedor proveedor;

	@CollectionView("Simple")
	@ListProperties("producto.descripcion, cantidad, precioUnitario, costo")
	@OneToMany(mappedBy = "comprobantePago", cascade = CascadeType.ALL)
	Collection<DetalleComprobantePago> detalleComprobantePago = new ArrayList<>()		
}
